#include "StageManager.h"
int WeaponCapsuleType[4] = {5, 3, 5, 4};
CStageManager::CStageManager()
{
}

CStageManager::~CStageManager()
{
}

void CStageManager::SetStage(int Stage)
{
	_Stage = Stage;
	ClearAllObject();
	ReadBackgroundFile();
	ReadObjectFile();
}

void CStageManager::ReadBackgroundFile()
{

}

void CStageManager::ReadObjectFile()
{
	char s[50];
	long Size;
	char *Data;
	sprintf(s,"resources\\Maps\\Object%d.txt",_Stage);
	FILE *file = fopen(s, "rb");

	if (!file)
	{
		printf("\nFile nguon co van de");
	}
	else
	{
		fseek(file, 0, SEEK_END);
		Size = ftell(file);
		fseek(file, 0, SEEK_SET);// fseek(file, size, 0);
		Data = new char[Size];
	}

	fread(Data, 1, Size, file);
	fclose(file);

	int i = 0;
	int Sum[5];
	int Count =-1;


	while(Data[i] != NULL)
	{
		if (Data[i] > 47 && Data[i] < 58)
		{
			Count = ((Count + 1) % 5);
			Sum[Count] = 0;

			while (Data[i + 1] > 47 && Data[i + 1] < 58)
			{
				Sum[Count] = Sum[Count] * 10 + Data[i] - 48;
				i++;
			}
			Sum[Count] = Sum[Count] * 10 + Data[i] - 48;
		}
		i++;

		if(Count == 4)
		{
			switch(Sum[0])
			{
				case 1:
				{
					_ListMapGround.push_back(new CMapGround(Sum[0], Sum[1], Sum[2], Sum[3], Sum[4]));
					break;
				}
				case 2:
				{
					_ListMapGround.push_back(new CMapGround(Sum[0], Sum[1], Sum[2], Sum[3], Sum[4]));
					break;
				}
				case 3:
				{
					_ListWeaponCapsule.push_back(new CWeaponCapsule(Sum[1], Sum[2], 0));
					break;
				}
				case 4:
				{
					break;
				}
				case 5:
				{

						  _ListEnemyRedForest.push_back(new CEnemyRedForest());
						  _ListEnemyRedForest.back()->LoadResource();
						  _ListEnemyRedForest.back()->SetPosition(Sum[1], Sum[2]);
					
					break;
				}
				case 6:
				{
					  _ListEnemyRed.push_back(new CEnemyRed());
					  _ListEnemyRed.back()->LoadResource();
					  _ListEnemyRed.back()->SetPosition(Sum[1], Sum[2]);

					break;
				}
				case 7:
				{
					break;
				}
				case 8:
				{
					break;
				}
				case 9:
				{
					_ListTurret.push_back(new CTurret(Sum[1], Sum[2]));
					break;
				}
				case 10:
				{
					_ListSuperTurret.push_back(new CSuperTurret(Sum[1], Sum[2]));
					break;
				}
				case 11:
				{
					break;
				}
				case 12:
				{
					break;
				}
				case 13:
				{
					break;
				}
				case 14:
				{
					break;
				}
				case 15:
				{
					break;
				}
				default:
					break;
			}
			Count = -1;
		}
	}
}

void CStageManager::ClearAllObject()
{
	_ListTurret.clear();
	_ListSuperTurret.clear();
	_ListWeaponCapsule.clear();
	_ListPowerUp.clear();
	_ListEnemyRed.clear();
	_ListEnemyWhite.clear();
	_ListExplosion.clear();
	_ListEnemyRedForest.clear();
	_ListMapGround.clear();
}

void CStageManager::Render(D3DXVECTOR2 ContraPosition)
{
	for(int i = 0; i < _ListTurret.size(); i++)
	{
		_ListTurret[i]->Render(ContraPosition);
	}

	for(int i = 0; i < _ListSuperTurret.size(); i++)
	{
		_ListSuperTurret[i]->Render(ContraPosition);
	}

	for(int i = 0; i < _ListEnemyRed.size(); i++)
	{
		_ListEnemyRed[i]->Render(ContraPosition);
	}

	for(int i = 0; i < _ListEnemyWhite.size(); i++)
	{
		_ListEnemyWhite[i]->Render(ContraPosition);
	}

	for(int i = 0; i < _ListWeaponCapsule.size(); i++)
	{
		_ListWeaponCapsule[i]->Render(ContraPosition);
	}
	for (int i = 0; i < _ListEnemyRedForest.size(); i++)
	{
		_ListEnemyRedForest[i]->Render(ContraPosition);
	}

	CExplosionManager::GetInstance()->Render();
}

vector<CCharacter*> CStageManager::getObjects()
{
	vector<CCharacter*> objects;
	return objects;
}